<?php

namespace App\Http\Controllers;

use App\Github;
use App\User;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Laravel\Socialite\Facades\Socialite;
use LaravelChen\MyFlash\MyFlash;

class LoginController extends Controller
{
    /**
     * 将用户重定向到Github认证页面
     *
     * @return Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver('github')->redirect();
    }

    /**
     * 从Github获取用户信息.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('github')->user();
        $uid = User::where("github_id",$user->getId())->first();
        if(!$uid)
        {
            $fly['name'] = $user->getNickname();
            $fly['email'] = $user->getEmail();
            $fly['avatar'] = $user->getAvatar();
            $fly['password'] = $user->token;
            $fly['github_id'] = $user->getId();
            $uid = User::create($fly);
        }
        Auth::loginUsingId($uid->id);
        return redirect('/user/setting');
    }

    public function index()
    {
        return view('login.index');
    }

    public function login(Request $request)
    {
        $vercode = request('vercode');
        if (Session::get('milkcaptcha') !== $vercode)
        {
            \myflash()->error('验证码错误','错误');
            return back()->withInput();
//            return back()->withErrors('验证码错误')->withInput();
        }

        $this->validate($request,[
            'email' => 'required|email',
            'password' => 'required|min:6|max:30',
            'is_remember' => '',
        ]);
        $user = request(['email','password']);
        $remember = boolval(request('is_remember'));
        if(true == \Auth::attempt($user,$remember))
        {
            return redirect('/posts');
        }
        return redirect()->back()->withErrors("用户名或密码错误")->withInput();
    }

    public function logout()
    {
        \Auth::logout();
        return redirect('/login');
    }

    public function captcha($tmp)
    {
        $build = new CaptchaBuilder();
        $build->build(100,36);
        $phr = $build->getPhrase();
        Session::flash('milkcaptcha',$phr);
        ob_clean();
        return response($build->output())->header('Content-type','image/jpeg');
    }
}
